import axios from '../axios.js'

// 用户相关API
export const userApi = {
  // 用户注册
  register: (userData) => {
    return axios.post('/user/register', userData)
  },
  
  // 用户登录
  login: (username, password) => {
    return axios.post('/user/login', null, {
      params: { username, password }
    })
  },
  
  // 获取用户信息
  getUserByUid: (uid) => {
    return axios.get('/user/get-uid', {
      params: { uid }
    })
  },
  
  // 获取用户信息（通过用户名）
  getUserByUsername: (username) => {
    return axios.get('/user/get-username', {
      params: { username }
    })
  },
  
  // 更新用户信息
  updateUserInfo: (uid, userInfo) => {
    return axios.put('/user/ud-info-uid', userInfo, {
      params: { uid }
    })
  },
  
  // 更新用户名
  updateUsername: (uid, newUsername) => {
    return axios.put('/user/ud-uname-uid', null, {
      params: { uid, newUsername }
    })
  },
  
  // 更新密码
  updatePassword: (uid, password) => {
    return axios.put('/user/ud-pwd-uid', null, {
      params: { uid, pwd: password }
    })
  },
  
  // 删除用户
  deleteUser: (uid) => {
    return axios.post('/user/delete-uid', null, {
      params: { uid }
    })
  }
}

// 购物车相关API
export const cartApi = {
  // 添加商品到购物车
  addToCart: (cartData) => {
    return axios.post('/cart/add', cartData)
  },
  
  // 获取用户购物车
  getUserCart: (userId) => {
    return axios.get(`/cart/user/${userId}`)
  },
  
  // 更新购物车商品数量
  updateQuantity: (updateData) => {
    return axios.put('/cart/update-quantity', updateData)
  },
  
  // 删除购物车商品
  deleteCartItem: (cid) => {
    return axios.delete(`/cart/item/${cid}`)
  },
  
  // 清空购物车
  clearCart: (userId) => {
    return axios.delete(`/cart/clear/${userId}`)
  },
  
  // 更新商品选中状态
  updateSelected: (cid, selected) => {
    return axios.put(`/cart/select/${cid}`, null, {
      params: { selected }
    })
  },
  
  // 批量更新选中状态
  batchUpdateSelected: (batchData) => {
    return axios.put('/cart/batch-select', batchData)
  },
  
  // 删除选中的商品
  deleteSelectedItems: (userId) => {
    return axios.delete(`/cart/delete-selected/${userId}`)
  },
  
  // 获取购物车汇总信息
  getCartSummary: (userId) => {
    return axios.get(`/cart/summary/${userId}`)
  },
  
  // 获取购物车商品数量
  getCartItemCount: (userId) => {
    return axios.get(`/cart/count/${userId}`)
  },
  
  // 获取选中商品数量
  getSelectedItemCount: (userId) => {
    return axios.get(`/cart/selected-count/${userId}`)
  },
  
  // 获取购物车总金额
  getTotalAmount: (userId) => {
    return axios.get(`/cart/total-amount/${userId}`)
  },
  
  // 获取选中商品总金额
  getSelectedAmount: (userId) => {
    return axios.get(`/cart/selected-amount/${userId}`)
  },
  
  // 全选购物车商品
  selectAllItems: (userId) => {
    return axios.put(`/cart/select-all/${userId}`)
  },
  
  // 取消全选购物车商品
  unselectAllItems: (userId) => {
    return axios.put(`/cart/unselect-all/${userId}`)
  }
}

// 工厂相关API
export const factoryApi = {
  // 创建工厂
  createFactory: (uid, factoryData) => {
    return axios.post('/factory/cre-factory', factoryData, {
      params: { uid }
    })
  },
  
  // 删除工厂
  deleteFactory: (uid, fid) => {
    return axios.put('/factory/del-factory-fid', null, {
      params: { uid, fid }
    })
  },
  
  // 更新工厂信息
  updateFactoryInfo: (uid, factoryData) => {
    return axios.put('/factory/ud-factory-uid', factoryData, {
      params: { uid }
    })
  },
  
  // 获取所有工厂
  getAllFactories: () => {
    return axios.get('/factory/get-factories')
  },
  
  // 通过关键词搜索工厂
  getFactoriesByKeyword: (keyword) => {
    return axios.get('/factory/get-factories-keyword', {
      params: { keyword }
    })
  },
  
  // 获取我的工厂
  getMyFactories: (uid) => {
    return axios.get('/factory/get-factories-my', {
      params: { uid }
    })
  }
}

// 订单相关API
export const orderApi = {
  // 创建订单
  createOrder: (orderData) => {
    return axios.post('/order/cre-order', orderData)
  },
  
  // 取消订单
  cancelOrder: (buyerId, orderId) => {
    return axios.put('/order/cancle-order', null, {
      params: { buyerId, orderId }
    })
  },
  
  // 交付订单
  fulfillOrder: (orderId, qty) => {
    return axios.put('/order/partial-order', null, {
      params: { orderId, qty }
    })
  },
  
  // 删除订单
  deleteOrder: (buyerId, orderId) => {
    return axios.put('/order/delete-order', null, {
      params: { buyerId, orderId }
    })
  },
  
  // 获取用户订单
  getUserOrders: (buyerId) => {
    return axios.get('/order/get-orders', {
      params: { buyerId }
    })
  },
  
  // 根据状态获取订单
  getOrdersByStatus: (buyerId, status) => {
    return axios.get('/order/get-orders-state', {
      params: { buyerId, status }
    })
  }
}

// 管理员相关API
export const adminApi = {
  // 查看需要变更的身份请求
  getRolesReview: (adminId) => {
    return axios.get('/admin/get-roles-review', {
      params: { adminId }
    })
  },
  
  // 审核身份
  reviewRoles: (adminUid, targetUid, newRole, approve) => {
    return axios.put('/admin/review-roles', null, {
      params: { adminUid, targetUid, newRole, approve }
    })
  },
  
  // 重置用户密码
  resetUserPassword: (adminUid, targetUid) => {
    return axios.post('/admin/reset-usr-pwd', null, {
      params: { adminUid, targetUid }
    })
  }
}

export default {
  userApi,
  cartApi,
  factoryApi,
  orderApi,
  adminApi
}
